﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace MagicFight
{
    public enum MagicType { Protection = 0, Holy, Arcane, Elemental }

    public abstract class Magic
    {
        public static PlayScreen ps;
        public Character caster;
        protected MagicType type;
        protected int manaCost;
        protected int pointCost;
        protected int castTime;
        public bool isCasting = true;
        public bool alive = true;
        protected double lifeTime = 0;

        public Magic(Character caster, int MagicNumber)
        {
            this.caster = caster;
            this.type = Def.MagicDefs[MagicNumber].Type;
            this.manaCost = Def.MagicDefs[MagicNumber].ManaCost;
            this.pointCost = Def.MagicDefs[MagicNumber].PointCost;
            this.castTime = Def.MagicDefs[MagicNumber].CastTime;
            if (caster == null || !caster.alive) { alive = false; return; }
            if (caster.isCasting) { Log.Add(caster.name + " nao pode castar magia enquanto estiver castando, sua mula!"); alive = false; return; }
            if (!caster.ConsumeMana(manaCost)) { Log.Add(caster.name + " nao tem mana suficiente, seu pobre!!!"); alive = false; return; } // se não tem mana suficiente, a mágica já morre
            if (!caster.AddPoints(type, pointCost)) { Log.Add(caster.name + " nao tem pontos suficientes, seu infeliz!!!"); caster.MP += manaCost; alive = false; return; } // se não tem pontos suficiente, a mágica já morre e devolve a mana usada
            caster.StartCastTime(castTime);
        }

        public virtual void Update(GameTime gameTime)
        {
            if (!alive) return;
            if (!caster.alive) { alive = false; return; }
            if (isCasting) Casting(gameTime);
            else
            {
                Effect(gameTime);
                lifeTime += gameTime.ElapsedGameTime.TotalMilliseconds;
            }
        }

        public virtual void Casting(GameTime gameTime)
        {
            if (caster.countered) { caster.castTimer = 1; caster.countered = false; alive = false; caster.isCasting = false; return; }
            caster.castTimer += gameTime.ElapsedGameTime.TotalMilliseconds;
            if (caster.castTimer > caster.castTime)
            {
                caster.castTimer = 1;
                isCasting = caster.isCasting = false;
            }
        }

        public abstract void Effect(GameTime gameTime);
    }


}
